package com.bib.ciglog;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

import android.view.View;
import android.view.View.MeasureSpec;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.bib.ciglog.data.CigObj;

public abstract class Utils {

	
	public static String getDate(CigObj cigObj) {

		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(cigObj.getDate());

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.FRANCE); 
		return sdf.format(c.getTime());
	}
	
	
	
	
	public static void setListViewHeightBasedOnChildren(ListView listView, int maxItems) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            // pre-condition
            return;
        }

        int totalHeight = 0;
        int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
        int nbItems = listAdapter.getCount();
        
        for (int i = 0; i < (nbItems < maxItems?nbItems:maxItems); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
        listView.requestLayout();
    }
}
